package com.ww.yaodao.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.ww.yaodao.annotation.RepeatAnnotation;
import com.ww.yaodao.model.bo.UserDO;
import com.ww.yaodao.model.vo.user.UserRespVO;
import com.ww.yaodao.service.RegisterService;
import com.ww.yaodao.service.UserService;
import com.ww.yaodao.util.*;
import com.ww.yaodao.model.vo.LoginVO;
import com.ww.yaodao.model.vo.RegisterVO;
import com.ww.yaodao.model.vo.user.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 用户模块
 */
@RestController
@RequestMapping("user")
@Slf4j
@Api(tags = "用户模块")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param loginVo
     * @return
     * @throws Exception
     */
    @PostMapping("login")
    @ApiOperation("登录")
    public Map<String, Object> loginUser(@RequestBody LoginVO loginVo) throws Exception {
        Map<String, Object> map = userService.login(loginVo);
        return map;
    }

    @Autowired
    private Producer producer;

    /**
     * 验证码
     *
     * @param response
     * @param session
     * @throws IOException
     */
    @GetMapping("/captcha.jpg")
    @ApiOperation(value = "验证码")
    public void getCaptcha(HttpServletResponse response, HttpSession session) throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        // 生成文字验证码
        String text = producer.createText();
        // 生成图片验证码
        BufferedImage image = producer.createImage(text);
        // 保存到验证码到 session
        session.setAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }


    /**
     * 查询用户
     *
     * @param userVo
     * @return
     */
    @PostMapping("queryUser")
    @ApiOperation("根据条件查询用户")
    public BaseResult<List<UserDO>> getUsers(@RequestBody UserVO userVo) {
        List<UserDO> list = userService.queryUser(userVo);
        if (list != null) {
            return BaseResult.success(list);
        } else {
            return BaseResult.error(ResultEnums.SELECT_ERROR);
        }
    }

    @GetMapping("{id}")
    @ApiOperation(value = "根据id删除用户")
    public ResponseData deleteUsersById(@PathVariable("id") @ApiParam(value = "用户id") Integer id) {
        int row = userService.deleteById(id);
        if (row > 0) {
            return ResponseDataUtil.buildOk(ResultEnums.DELETED);
        } else {
            return ResponseDataUtil.buildError("删除失败");
        }
    }

    @Autowired
    private RegisterService registerService;

    /**
     * 注册用户
     * @param registerVO
     * @return
     */
    @PostMapping("register")
    @ApiOperation("注册用户")
    public ResponseData registerUser(@RequestBody RegisterVO registerVO) {
        int i = registerService.addOneUser(registerVO);
        if (i>0){
            return ResponseDataUtil.buildOk(ResultEnums.CREATED);
        }else {
            return ResponseDataUtil.buildOk(ResultEnums.REGISTER_ERROR);
        }
    }

    @GetMapping("token")
    @RepeatAnnotation
    public ResponseData getOrderToken(HttpServletRequest request){
        String token = request.getHeader("token");
        //key的组成
        return ResponseDataUtil.buildOk(token);
    }

    @PostMapping("/addPerson")
    @ApiOperation("修改人员信息")
    public UserRespVO addPreson(@RequestBody UserVO userVO){
        log.info("进入添加用户的页面");
        return userService.exitPerson(userVO);
    }
}
